package com.mef.smsplat.web.controller.sys.user;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hc24.util.IDUtil;
import com.hc24.util.StringUtil;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.AdminInfoDomain;
import com.mef.smsplat.domain.OperatorInfoDomain;
import com.mef.smsplat.domain.SysAdminInfoDomain;
import com.mef.smsplat.exception.BusinessException;
import com.mef.smsplat.service.OperatorInfoService;
import com.mef.smsplat.util.Constants;
import com.mef.smsplat.web.controller.base.Json;

@Controller
@RequestMapping("/sys/operate")
public class OperatorInfoController {
	private OperatorInfoService operatorInfoService;
	
	public void setOperatorInfoService(OperatorInfoService operatorInfoService) {
		this.operatorInfoService = operatorInfoService;
	}

	@ResponseBody
	@RequestMapping("/login.sys")
	public Json login(HttpSession session,String operatorName,String operatorPassword){
		if(operatorName==null||"".equals(operatorName.trim()))
			throw new BusinessException("用户名不能为空!");
		if(operatorPassword==null||"".equals(operatorPassword.trim()))
			throw new BusinessException("密码不能为空!");
		OperatorInfoDomain operator= operatorInfoService.login(operatorName, operatorPassword);
		if(operator!=null){
			session.setAttribute(Constants.SESSION_TYPE, Constants.SESSION_OPERATOR);
			session.setAttribute(Constants.SESSION_OPERATOR, operator);
			Json json=new Json();
			json.setData(operator);
			return json;
			
		}else{
			Json json=new Json();
			json.setSuccess(false);
			json.setMsg("用户名或者密码错误!");
			return json;
		}
	}
	
	@ResponseBody
	@RequestMapping("/query.sys")
	public Page<OperatorInfoDomain> query(String operatorName,DataGridModel dgm){
		return operatorInfoService.query(operatorName, dgm);
	}
	
	@ResponseBody
	@RequestMapping("/add.sys")
	public Json add(HttpSession session,OperatorInfoDomain operator){
		if(operator.getOperatorName()==null || "".equals(operator.getOperatorName().trim())){
			throw new BusinessException("用户名不能为空");
		}
		if(operator.getTrueName()==null || "".equals(operator.getTrueName().trim())){
			throw new BusinessException("真实姓名不能为空");
		}
		if(operator.getDepart()==null || "".equals(operator.getDepart().trim())){
			throw new BusinessException("部门不能为空");
		}
		if(operator.getTel()==null || "".equals(operator.getTel().trim())){
			throw new BusinessException("联系电话不能为空");
		}
		if(operator.getValidBeginTime()==null){
			throw new BusinessException("开始时间不能为空");
		}
		if(operator.getValidEndTime()==null){
			throw new BusinessException("结束时间不能为空");
		}
		if(!operatorInfoService.isUnique(operator.getOperatorName(), operator.getOperatorId())){
			throw new BusinessException("此账号已经存在");
		}
		//获取当前登录用户  并且获得createId 和 createName
		String createId = "";
		String createName = "";
		if(Constants.SESSION_SYS_ADMIN.equals(session.getAttribute(Constants.SESSION_TYPE))){
			SysAdminInfoDomain adm = (SysAdminInfoDomain) session.getAttribute(Constants.SESSION_SYS_ADMIN);
			createId = adm.getSysAdminId();
			createName = adm.getAdminName();
		}else if(Constants.SESSION_ADMIN.equals(session.getAttribute(Constants.SESSION_TYPE))){
			AdminInfoDomain adm = (AdminInfoDomain) session.getAttribute(Constants.SESSION_ADMIN);
			createId = adm.getAdminId();
			createName = adm.getAdminName();
		}else if(Constants.SESSION_OPERATOR.equals(session.getAttribute(Constants.SESSION_TYPE))){
			OperatorInfoDomain ope = (OperatorInfoDomain) session.getAttribute(Constants.SESSION_OPERATOR);
			createId = ope.getOperatorId();
			createName = ope.getOperatorName();
		}else{
			createId = "1";
			createName = "no";
		}
		
		operator.setOperatorId(IDUtil.next()+"");
		operator.setOperatorPassword(StringUtil.md5(Constants.PASSWORD));//默认密码
		operator.setCreateTime(new Date());
		operator.setCreateId(createId);
		operator.setCreateName(createName);
		operator.setStatus(Constants.NORMAL);
		operatorInfoService.save(operator);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/update.sys")
	public Json update(OperatorInfoDomain operator){
		if(operator.getOperatorName()==null || "".equals(operator.getOperatorName().trim())){
			throw new BusinessException("用户名不能为空");
		}
		if(operator.getTrueName()==null || "".equals(operator.getTrueName().trim())){
			throw new BusinessException("真实姓名不能为空");
		}
		if(operator.getDepart()==null || "".equals(operator.getDepart().trim())){
			throw new BusinessException("部门不能为空");
		}
		if(operator.getTel()==null || "".equals(operator.getTel().trim())){
			throw new BusinessException("联系电话不能为空");
		}
		if(operator.getValidBeginTime()==null){
			throw new BusinessException("开始时间不能为空");
		}
		if(operator.getValidEndTime()==null){
			throw new BusinessException("结束时间不能为空");
		}
		if(!operatorInfoService.isUnique(operator.getOperatorName(), operator.getOperatorId())){
			throw new BusinessException("此账号已经存在");
		}
		operatorInfoService.update(operator);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/del.sys")
	public Json del(String[] ids){
		operatorInfoService.removeById(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/resetPassword.sys")
	public Json resetPassword(String[] ids){
		operatorInfoService.resetPassword(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/enable.sys")
	public Json enable(String[] ids){
		operatorInfoService.enable(ids);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/disable.sys")
	public Json disable(String[] ids){
		operatorInfoService.disable(ids);
		return new Json();
	}
}
